package com.pq.controller;

import com.pq.pojo.order.OrderCart;
import com.pq.pojo.product.Product;
import com.pq.pojo.user.User;
import com.pq.service.order.OrderCartService;
import com.pq.utils.CookieUtil;
import com.pq.utils.ObjectMapperUtil;
import com.pq.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

@Controller
public class OrderCartController {
    private static final String PQ_TICKET = "PQ_TICKET";

    @Autowired
    private OrderCartService orderCartService;

    @Autowired
    private Jedis jedis;

    @ResponseBody
    @PostMapping("/doFindOrderCartByUserId")
    public SysResult doFindOrderCartByUserId(String flag, HttpServletRequest request) {
        String ticket = CookieUtil.getCookieValue(request, PQ_TICKET);
        String json = jedis.get(ticket);
        User user = ObjectMapperUtil.toObject(json, User.class);
        int userId = user.getUserId();
        String productId = jedis.get("productId");
        List<OrderCart> orderCarts = orderCartService.findOrderCartByProductId(userId, Integer.valueOf(productId));
        List<Long> orderCartIds = orderCarts.stream().map(obj -> obj.getOrderCartId()).collect(Collectors.toList());
        /*List<Long> orderCartIds = new ArrayList<>();
        orderCartIds.add(55L);
        orderCartIds.add(56L);*/
        List<OrderCart> list = orderCartService.findOrderCartByUserId(userId, Integer.valueOf(flag), orderCartIds);
        return SysResult.success(list);
    }


    @ResponseBody
    @RequestMapping("/doUpdateOrderCartCountsByCartId")
    public SysResult updateOrderCartCountsByCartId(String productCounts, String cartId){
        int userId = 2;
        orderCartService.updateOrderCartCountsByCartId(Integer.valueOf(productCounts),
                                                       Integer.valueOf(cartId), userId);
        return SysResult.success("200", null);
    }

    @ResponseBody
    @PostMapping("/doSaveOrder")
    public SysResult doSaveOrder(String orderCartIds) {
        String orderId = jedis.get("orderId");
        return SysResult.success("200", orderId);
    }

    @GetMapping("/orderSubmit")
    public String orderSubmit(String orderId, Model model) {
        model.addAttribute("orderId", orderId);
        model.addAttribute("siteId", "1");
        return "ordersubmit";
    }

    @RequestMapping("/doFindOrderCartByProductId")
    public SysResult findOrderCartByProductId(Integer userId){
        int productId = 2;
        return SysResult.success(orderCartService.findOrderCartByProductId(userId, productId));
    }

    @ResponseBody
    @PostMapping("/doSaveOrderPay")
    public SysResult doSaveOrderPay(String orderId, String siteId) {
        return SysResult.success();
    }

    @GetMapping("/order/order_finish")
    public String orderFinish(String orderId) {
        return "order/order_finish";
    }

    @ResponseBody
    @RequestMapping("/doUpdateOrderCartFlagByCartId")
    public SysResult updateOrderCartFlagByCartId(Long cartId){
        orderCartService.deleteByCartId(cartId);
        return SysResult.success();
    }

    @RequestMapping("/doUpdateOrderCartFlagByCartIds")
    public SysResult updateOrderCartFlagByCartIds(List<Long> orderCartIds){
        int userId = 2;
        return orderCartService.updateOrderCartFlagByCartIds(userId, orderCartIds);
    }
}
